package com.yanxx.codegenerator.service;

import com.yanxx.codegenerator.CodeGenerator;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Set;

/**
 * 编码生成器服务
 */
@Service
public class CodeGeneratorService {

    private final CodeGenerator codeGenerator;

    public CodeGeneratorService(CodeGenerator codeGenerator) {
        this.codeGenerator = codeGenerator;
    }

    /**
     * 生成新编码
     *
     * @return 生成的编码
     */
    public String generateCode() {
        return codeGenerator.generate();
    }

    /**
     * 确认编码已使用
     *
     * @param code 要确认的编码
     * @return 确认是否成功
     */
    public boolean confirmCode(String code) {
        return codeGenerator.confirm(code);
    }

    /**
     * 回收未使用的编码
     *
     * @param code 要回收的编码
     * @return 回收是否成功
     */
    public boolean recycleCode(String code) {
        return codeGenerator.recycle(code);
    }

    /**
     * 获取可用的回收编码数量
     *
     * @return 可用的回收编码数量
     */
    public int getAvailableCount() {
        return codeGenerator.getAvailableCount();
    }

    /**
     * 获取所有待确认的编码
     *
     * @return 待确认的编码集合
     */
    public Set<String> getPendingCodes() {
        if (codeGenerator instanceof com.yanxx.codegenerator.impl.SequentialCodeGenerator) {
            return ((com.yanxx.codegenerator.impl.SequentialCodeGenerator) codeGenerator).getPendingCodes();
        }
        return Collections.emptySet();
    }

    /**
     * 获取所有已确认的编码
     *
     * @return 已确认的编码集合
     */
    public Set<String> getConfirmedCodes() {
        if (codeGenerator instanceof com.yanxx.codegenerator.impl.SequentialCodeGenerator) {
            return ((com.yanxx.codegenerator.impl.SequentialCodeGenerator) codeGenerator).getConfirmedCodes();
        }
        return Collections.emptySet();
    }

    /**
     * 获取所有已回收的编码
     *
     * @return 已回收的编码集合
     */
    public Set<String> getRecycledCodes() {
        if (codeGenerator instanceof com.yanxx.codegenerator.impl.SequentialCodeGenerator) {
            return ((com.yanxx.codegenerator.impl.SequentialCodeGenerator) codeGenerator).getRecycledCodes();
        }
        return Collections.emptySet();
    }
}